package com.sc.nft.controller;


import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.*;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.PledgeTaskInfoService;
import com.sc.nft.service.PledgeTaskVigorousCollectionService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/secret/area")
@Api(tags = "秘境任务")
public class PledgeTaskController {

    private final PledgeTaskInfoService pledgeTaskInfoService;
    private final PledgeTaskVigorousCollectionService pledgeTaskVigorousCollectionService;
    private final Redisson redisson;

    @ApiOperation(value = "秘境列表", notes = "秘境列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/secretAreaPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<SecretAreaPageVO> secretAreaPage(@Valid @RequestBody PageRequest request) {
        return PageResult.success(pledgeTaskInfoService.secretAreaPage(UserPool.getUser(),request.getPageNo(),request.getPageSize()));
    }

    @ApiOperation(value = "秘境激活详情", notes = "秘境激活详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/secretAreaVigorousDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<SecretAreaVigorousDetailsVO> secretAreaVigorousDetails(@Valid @RequestBody IdRequest request) {
        return Result.success(pledgeTaskInfoService.secretAreaVigorousDetails(request.getId(), UserPool.getUser()));
    }


    @ApiOperation(value = "获取用户激活/质押的NFT列表", notes = "选取激活的NFT列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/secretAreaVigorousCollectionPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<UserHoldCollectionVO> secretAreaVigorousCollectionPageById(@Valid @RequestBody SecretAreaVigorousCollectionPageByIdRequest request) {
        return PageResult.success(pledgeTaskVigorousCollectionService.secretAreaVigorousDetails(request.getPledgeTaskId(),request.getId(),request.getType(), request.getCollectionId(),UserPool.getUser(),request.getPageNo(),request.getPageSize()));
    }

    @ApiOperation(value = "激活秘境", notes = "激活秘境", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/vigorousSecretArea", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result vigorousSecretArea(@Valid @RequestBody VigorousSecretAreaRequest request) {
        UserInfo user = UserPool.getUser();
        if(!StrUtil.equals( request.getPayPwd(),user.getTransactionPassword())){
            return Result.fail(ErrorCode.PASSWORD_ERROR);
        }


        RLock lock = redisson.getLock(RedisKeyEnum.UNEXPLORED_REGION_LOCK.getKey() + request.getPledgeTaskId());
        try {
            boolean res = lock.tryLock(10, 10, TimeUnit.SECONDS);
            if (res) {
                pledgeTaskInfoService.vigorousSecretArea(user,request.getPledgeTaskId(),request.getVigorousCollectionList());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new GlobalRunTimeException("系统超时");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }


    @ApiOperation(value = "秘境质押详情", notes = "秘境质押详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/secretAreaCollectionDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<PledgeTaskCollectionVO>> secretAreaCollectionDetails(@Valid @RequestBody IdRequest request) {
        return Result.success(pledgeTaskInfoService.secretAreaCollectionDetails(request.getId(), UserPool.getUser()));
    }

    @ApiOperation(value = "质押秘境", notes = "质押秘境", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pledgeSecretArea", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result pledgeSecretArea(@Valid @RequestBody PledgeSecretAreaRequest request) {
        UserInfo user = UserPool.getUser();
        if(!StrUtil.equals( request.getPayPwd(),user.getTransactionPassword())){
            return Result.fail(ErrorCode.PASSWORD_ERROR);
        }

        RLock lock = redisson.getLock(RedisKeyEnum.PLEDGE_COLLECTION_SECRET_AREA_LOCK.getKey() + request.getPledgeTaskCollectionId());
        try {
            boolean res = lock.tryLock(10, 10, TimeUnit.SECONDS);
            if (res) {
                pledgeTaskInfoService.batchPledgeSecret(user, request.getPledgeTaskId(), request.getPledgeTaskCollectionId(), request.getUserCollectionIdList(),0);
            } else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new GlobalRunTimeException("系统超时");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @ApiOperation(value = "批量质押秘境", notes = "批量质押秘境", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/batchPledgeSecret", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result batchPledgeSecret(@Valid @RequestBody BatchPledgeSecretAreaRequest request) {
        UserInfo user = UserPool.getUser();
        if (!StrUtil.equals(request.getPayPwd(), user.getTransactionPassword())) {
            return Result.fail(ErrorCode.PASSWORD_ERROR);
        }

        RLock lock = redisson.getLock(RedisKeyEnum.PLEDGE_COLLECTION_SECRET_AREA_LOCK.getKey() + request.getPledgeTaskCollectionId());
        try {
            boolean res = lock.tryLock(10, 10, TimeUnit.SECONDS);
            if (res) {
                pledgeTaskInfoService.batchPledgeSecret(user, request.getPledgeTaskId(), request.getPledgeTaskCollectionId(),null,request.getPledgeCount());
            } else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            throw new GlobalRunTimeException("系统超时");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @ApiOperation(value = "用户最大质押份数", notes = "用户最大质押份数", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pledgeSecretMaxCount", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<PledgeSecretMaxCount> pledgeSecretMaxCount(@Valid @RequestBody PledgeSecretMaxCountRequest request){
        return Result.success(pledgeTaskInfoService.pledgeSecretMaxCount(request.getPledgeTaskId(),request.getCollectionId(), UserPool.getUser()));
    }

    //退出秘境 quit
    @ApiOperation(value = "退出秘境", notes = "退出秘境", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/quitSecretArea", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result quitSecretArea(@Valid @RequestBody QuitSecretAreaRquest request) {

        UserInfo user = UserPool.getUser();
        if(!StrUtil.equals( request.getPayPwd(),user.getTransactionPassword())){
            return Result.fail(ErrorCode.PASSWORD_ERROR);
        }

        RLock lock = redisson.getLock(RedisKeyEnum.QUIT_SECRET_AREA_LOCK.getKey() + request.getPledgeTaskId());
        try {
            boolean res = lock.tryLock(10, 10, TimeUnit.SECONDS);
            if (res) {
                pledgeTaskInfoService.quitSecretArea(user,request.getPledgeTaskId());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new GlobalRunTimeException("系统超时");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }
    //激活记录
    @ApiOperation(value = "秘境激活记录", notes = "秘境激活记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/vigorousSecretAreaPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<VigorousSecretAreaPageVO> vigorousSecretAreaPage(@Valid @RequestBody PageRequest request) {
        return PageResult.success(pledgeTaskInfoService.vigorousSecretAreaPage(UserPool.getUser(),request.getPageNo(),request.getPageSize()));
    }


    @ApiOperation(value = "秘境激活记录详情", notes = "秘境激活记录详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/vigorousSecretAreaDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<VigorousSecretAreaDetailsVO> vigorousSecretAreaDetails(@Valid @RequestBody VigorousSecretAreaDetailsRequest request) {
        return Result.success(pledgeTaskInfoService.vigorousSecretAreaDetails(UserPool.getUser(),request.getPledgeTaskId(),request.getUserVigorousRecordsId()));
    }


    @ApiOperation(value = "挖宝记录", notes = "挖宝记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pledgeSecretAreaPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<PledgeSecretAreaPageVO> pledgeSecretAreaPage(@Valid @RequestBody PledgeSecretAreaPageRequest request) {
        return PageResult.success(pledgeTaskInfoService.pledgeSecretAreaPage(UserPool.getUser(),request.getPledgeTaskId(),request.getPageNo(),request.getPageSize()));
    }


    @ApiOperation(value = "挖宝记录详情", notes = "挖宝记录详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pledgeSecretAreaDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<PledgeSecretAreaDetailsVO> pledgeSecretAreaDetails(@Valid @RequestBody PledgeSecretAreaDetailsRequest request) {
        return Result.success(pledgeTaskInfoService.pledgeSecretAreaDetails(UserPool.getUser(),request.getPledgeTaskId(),request.getUserPledgeRecordsId()));
    }



}
